<?php

@date_default_timezone_set('PRC');
header('Content-type: text/html; charset=utf8');
//定义时间

if($_SERVER['REQUEST_METHOD'] == "POST"){
	
	include "Conn.php";
	include "function.php";
	
	$Type = trim(isset($_POST["Type"])?$_POST["Type"]:'html');//请求类型
	$UserName = trim(isset($_POST["UserName"])?$_POST["UserName"]:'');//请求类型
	$PassWord = trim(isset($_POST["PassWord"])?$_POST["PassWord"]:'');//请求类型
	$Email = trim(isset($_POST["Email"])?$_POST["Email"]:'');//请求类型
	$PassWordTwo = trim(isset($_POST["PassWordTwo"])?$_POST["PassWordTwo"]:'');//请求类型
	
	if( $Type == "html"){
		
			if(empty($UserName)){
				$msg = array('code'=>121,"msg"=>'用户名为空');
				TuiChu($msg ,$Type);
			}
			if(empty($PassWord)){
				$msg = array('code'=>122,"msg"=>'密码为空');
				TuiChu($msg ,$Type);
			}
			if(empty($Email)){
				$msg = array('code'=>123,"msg"=>'邮箱为空');
				TuiChu($msg ,$Type);
			}
			if(empty($PassWordTwo)){
				$msg = array('code'=>124,"msg"=>'重复密码为空');
				TuiChu($msg ,$Type);
			}
			if($PassWord!= $PassWordTwo)){
				$msg = array('code'=>125,"msg"=>'重复密码错误');
				TuiChu($msg ,$Type);
			}
			
			if(!preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $Email)){
				$msg = array('code'=>126,"msg"=>'邮箱错误');
				TuiChu($msg ,$Type);
			}
			
			$sql = "SELECT * FROM User WHERE username = '$UserName'";
			$res = mysql_query($sql,$con);
			$rows=mysql_num_rows($res);
			if($rows)){
				$msg = array('code'=>127,"msg"=>'用户名已存在');
				TuiChu($msg ,$Type);
			}
			
			$exec = "INSERT INTO UserName (username,password,email) values ('".$UserName."','".$PassWord."','".$Email."')";  
			
			if(mysql_query($exec, $con)){
				$msg = array('code'=>128,"msg"=>'用户注册成功');
				TuiChu($msg ,$Type);
			}else{
				$msg = array('code'=>129,"msg"=>'用户注册失败，请联系管理员！');
				TuiChu($msg ,$Type);
			}
	
	}
	
}else{
	echo "请正确访问页面";
	exit;
}